#
# (C) Copyright 2006-2007
# Stefan Roese, DENX Software Engineering, sr@denx.de.
#
# (C) Copyright 2008
# Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
#
# See file CREDITS for list of people who contributed to this
# project.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#

CONFIG_NAND_SPL	= y
CONFIG_PRELOADER = y

include $(TOPDIR)/config.mk
include $(TOPDIR)/nand_spl/board/$(BOARDDIR)/config.mk

LDSCRIPT= $(TOPDIR)/nand_spl/board/$(BOARDDIR)/u-boot.lds
LDFLAGS	= -Bstatic -T $(nandobj)u-boot.lds -Ttext $(TEXT_BASE) $(PLATFORM_LDFLAGS)
AFLAGS	+= -DCONFIG_NAND_SPL
AFLAGS	+= -DCONFIG_PRELOADER
CFLAGS	+= -DCONFIG_NAND_SPL
CFLAGS	+= -DCONFIG_PRELOADER

SOBJS	= start.o cpu_init.o lowlevel_init.o reset.o ioinit.o mmu.o cache_flush.o

COBJS-y	+= boot_main.o
COBJS-y	+= timer.o
COBJS-y$(CONFIG_SYS_BOOT_NOR) += nand_boot.o
COBJS-y$(CONFIG_SYS_BOOT_NOR)	+= crc32.o
COBJS-y$(CONFIG_SYS_BOOT_NOR)	+= img_check.o
COBJS-y$(CONFIG_SYS_BOOT_NOR) += ix_nand.o
COBJS-y$(CONFIG_DISABLE_BOOT_OTG)$(CONFIG_SYS_BOOT_NOR) += ix_udc.o
COBJS-$(CONFIG_BOOT_AUTO_ADJUST) += denali_config.o
ifdef CONFIG_SYS_EX_INIT_GPIO
COBJS-y += ex_ioinit.o
endif

#COBJS	= nand_boot.o nand_ecc.o imapx200.o denali_test.o
COBJS	:= $(COBJS-y)

SRCS	:= $(addprefix $(obj),$(SOBJS:.o=.S) $(COBJS:.o=.c))

COBJS-$(CONFIG_BOOT_AUTO_ADJUST) += denali.o
COBJS	:= $(COBJS-y)

OBJS	:= $(addprefix $(obj),$(SOBJS) $(COBJS))
__OBJS	:= $(SOBJS) $(COBJS)
LNDIR	:= $(OBJTREE)/nand_spl/board/$(BOARDDIR)

nandobj	:= $(OBJTREE)/nand_spl/

ALL	= $(nandobj)u-boot-spl $(nandobj)u-boot-spl.bin $(nandobj)u-boot-spl-16k.bin

all:	$(obj).depend $(ALL)

$(nandobj)u-boot-spl-16k.bin: $(nandobj)u-boot-spl
	$(OBJCOPY) ${OBJCFLAGS} --pad-to=$(OFFS_PAD_TO) -O binary $< $@.tmp
	@dd if=$@.tmp of=$@.partl bs=1K skip=4 count=4
	@cat $@.tmp $@.partl $@.partl > $@
	@rm $@.partl $@.tmp

$(nandobj)u-boot-spl.bin:	$(nandobj)u-boot-spl
	$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@

$(nandobj)u-boot-spl:	$(OBJS) $(nandobj)u-boot.lds
	cd $(LNDIR) && $(LD) $(LDFLAGS) $(__OBJS) \
		-Map $(nandobj)u-boot-spl.map \
		-o $(nandobj)u-boot-spl

$(nandobj)u-boot.lds: $(LDSCRIPT)
	$(CPP) $(CPPFLAGS) $(LDPPFLAGS) -ansi -D__ASSEMBLY__ -P - <$^ >$@

# create symbolic links for common files

# from cpu directory
$(obj)start.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/start.S $@

# from SoC directory
$(obj)cpu_init.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/cpu_init.S $@

$(obj)reset.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/reset.S $@

$(obj)mmu.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/mmu.S $@

$(obj)cache_flush.S:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/cache_flush.S $@

$(obj)timer.c:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/timer.c $@

$(obj)denali_config.c:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/denali_config.c $@

$(obj)denali.o:
	@rm -f $@
	@ln -s $(TOPDIR)/cpu/arm1136/imapx200/denali.c $@

# from board directory
$(obj)lowlevel_init.S:
	@rm -f $@
	@ln -s $(TOPDIR)/board/infotm/imapx/lowlevel_init.S $@

# from drivers/mtd/nand directory
$(obj)ix_nand.c:
	@rm -f $@
	@ln -s $(TOPDIR)/drivers/mtd/nand/imapx200.c $@

$(obj)ix_udc.c:
	@rm -f $@
	@ln -s $(TOPDIR)/oem/udc.c $@

ifneq ($(OBJTREE),$(SRCTREE))
$(obj)preloader.h:
	@rm -f $@
	@ln -s $(src)preloader.h $@

$(obj)boot_main.c: $(obj)preloader.h
	@rm -f $@
	@ln -s $(src)boot_main.c $@

$(obj)crc32.c:
	@rm -f $@
	@ln -s $(src)crc32.c $@

$(obj)img_check.c:
	@rm -f $@
	@ln -s $(src)img_check.c $@

$(obj)nand_boot.c:
	@rm -f $@
	@ln -s $(src)nand_boot.c $@

$(obj)ex_ioinit.c:
	@rm -f $@
	@ln -s $(src)ex_ioinit.c $@

$(obj)ioinit.S:
	@rm -f $@
	@ln -s $(src)ioinit.S $@
endif

#########################################################################

$(obj)%.o:	$(obj)%.S
	$(CC) $(AFLAGS) -c -o $@ $<

$(obj)%.o:	$(obj)%.c
	$(CC) $(CFLAGS) -c -o $@ $<

# defines $(obj).depend target
include $(SRCTREE)/rules.mk

sinclude $(obj).depend

#########################################################################
